package com.jnu.ilearn;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jnu.ilearn.bean.DataBank;
import com.jnu.ilearn.bean.PointRecord;
import com.jnu.ilearn.bean.User;
import com.jnu.ilearn.utils.DataLoader;

import java.util.ArrayList;
import java.util.List;

public class personal_center extends AppCompatActivity {

    List<PointRecord> pointRecordList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_center);

        getSupportActionBar().hide();
        ImageView backView=findViewById(R.id.back);
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView userNameView=findViewById(R.id.user_name);
        TextView pointNumber=findViewById(R.id.Point);
        DataBank dataBank=new DataBank(personal_center.this);
        dataBank.loadData();
        pointRecordList=dataBank.getPointRecordList();
        User user=dataBank.getUser();
        userNameView.setText(user.getAlias());
        pointNumber.setText(user.getPoint()+"");
        RecyclerView PointRecycleView=findViewById(R.id.PointRecordRecycleView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(personal_center.this);
        PointRecycleView.setLayoutManager(layoutManager);
        PointRecycleView.setAdapter(new MyRecyclerViewAdapter(pointRecordList));
    }

    private class MyRecyclerViewAdapter extends RecyclerView.Adapter {
        private List<PointRecord> pointRecordList;
        public MyRecyclerViewAdapter(List<PointRecord> pointRecordList) {
            this.pointRecordList=pointRecordList;
        }


        public  class ViewHolder extends RecyclerView.ViewHolder


        {

            private TextView messageView;
            private TextView numberView;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);

                this.setMessageView(itemView.findViewById(R.id.record_message));
                this.setNumberView(itemView.findViewById(R.id.record_number));
            }

            public TextView getMessageView() {
                return messageView;
            }

            public void setMessageView(TextView messageView) {
                this.messageView = messageView;
            }

            public TextView getNumberView() {
                return numberView;
            }

            public void setNumberView(TextView numberView) {
                this.numberView = numberView;
            }
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.point_record_item, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ViewHolder viewHolder=(ViewHolder) holder;

            viewHolder.getMessageView().setText(pointRecordList.get(position).getMessage());
            if(pointRecordList.get(position).getMoney()>0){
                viewHolder.getNumberView().setText("+"+pointRecordList.get(position).getMoney());
            }
            else{
                viewHolder.getNumberView().setText(pointRecordList.get(position).getMoney()+"");
            }

        }

        @Override
        public int getItemCount() {
            return pointRecordList.size();
        }
    }
}